package admin

import (
	"go-middle/app/admin/api"
	"go-middle/app/admin/api/adminapi"
	"go-middle/app/admin/api/ciapi"
	"go-middle/app/admin/api/cronapi"
	"go-middle/app/admin/api/systemapi"
	"go-middle/app/admin/service/adminservice"
	"go-middle/app/admin/service/loginservice"
	"go-middle/app/admin/web"
	"go-middle/app/admin/web/adminweb"
	"go-middle/app/admin/web/ciweb"
	"go-middle/app/admin/web/cronweb"
	"go-middle/app/admin/web/systemweb"
	"go-middle/app/common/middleware"
	"go-middle/app/common/model/admin"
	"go-middle/library/token"

	"github.com/gogf/gf/frame/g"
	"github.com/gogf/gf/net/ghttp"
)

//InitRoute 后台管理路由
func InitRoute() {
	s := g.Server()
	s.BindHandler("/test", web.Test)
	//web
	s.Group("/admin", func(group *ghttp.RouterGroup) {
		//验证token
		group.Middleware(func(r *ghttp.Request) {
			v := g.View("admin")
			v.BindFunc("auth", func(method, route string) bool {
				if r.Session.GetInt64("uid") != 1 {
					var user admin.Entity
					_ = r.Session.GetStruct(loginservice.AdminInfo, &user)
					return adminservice.CheckPermission(user.Roles, route, method)
				}
				return true
			})
			r.SetView(v)
			r.Middleware.Next()
		}, token.Init)
		group.GET("/login", web.Login)
		group.Group("/", func(group *ghttp.RouterGroup) {
			//需要登录
			group.Middleware(middleware.CheckLoginAdminWeb)
			group.GET("/", systemweb.Index)
			//系统管理
			group.Group("/system", func(group *ghttp.RouterGroup) {
				group.GET("/", systemweb.Info)
				group.GET("/home", systemweb.Home)
				//用户
				group.GET("/admin", systemweb.Admin)
				group.GET("/admin_edit", systemweb.AdminEdit)
				//菜单
				group.GET("/menu", systemweb.Menu)
				group.GET("/menu_edit", systemweb.MenuEdit)
				//角色
				group.GET("/role", systemweb.Role)
				//权限树
				group.GET("/permission", systemweb.Permission)
			})
			//管理员信息
			group.Group("/admin", func(group *ghttp.RouterGroup) {
				group.GET("change_pwd", adminweb.AdminChangePwd)
				group.GET("change_info", adminweb.AdminChangeInfo) //未完善
			})
			//定时任务
			group.Group("/cron", func(group *ghttp.RouterGroup) {
				group.GET("/", cronweb.CronTab)
				group.GET("/edit", cronweb.CronEdit)
			})
			group.Group("/cron_log", func(group *ghttp.RouterGroup) {
				group.GET("/", cronweb.CronLogTab)
			})
			//ci任务
			group.Group("/ci", func(group *ghttp.RouterGroup) {
				group.GET("/", ciweb.List)
				group.GET("/edit", ciweb.CiEdit)
			})
			group.Group("/ci_log", func(group *ghttp.RouterGroup) {
				group.GET("/", ciweb.CiLogList)
			})
		})
	})

	// api
	s.Group("/api", func(group *ghttp.RouterGroup) {
		group.GET("/token", api.Token)
		group.Group("/", func(group *ghttp.RouterGroup) {
			//验证token
			group.Middleware(token.Init)
			group.GET("/captcha", api.Captcha)
			group.POST("/login", api.Login)
			group.POST("/logout", api.Logout)
			group.Group("/", func(group *ghttp.RouterGroup) {
				//需要登录
				group.Middleware(middleware.CheckLoginAdminApi)
				//系统管理
				group.Group("/system", func(group *ghttp.RouterGroup) {
					//清缓存
					group.GET("clear", systemapi.Clear)
					//菜单
					group.GET("menu", systemapi.Menu)
					group.POST("menu", systemapi.MenuAdd)
					group.PUT("menu", systemapi.MenuEdit)
					group.DELETE("menu", systemapi.MenuDelete)
					//权限
					group.GET("permission", systemapi.Permission)
					//角色
					group.GET("role", systemapi.Role)
					group.DELETE("role", systemapi.RoleDelete)
					group.PUT("role", systemapi.RoleEdit)
					group.POST("role", systemapi.RoleAdd)
					group.DELETE("role/user", systemapi.RoleRemoveUser)
					group.POST("role/user", systemapi.RoleAddUser)
					group.GET("role/permission", systemapi.RolePermission)
					group.PUT("role/permission", systemapi.RolePermissionEdit)
					//用户
					group.GET("admin", systemapi.Admin)
					group.POST("admin", systemapi.AdminAdd)
					group.PUT("admin", systemapi.AdminEdit)
					group.DELETE("admin", systemapi.AdminDelete)
					group.POST("admin/reset_pwd", systemapi.AdminResetPwd)
				})
				//管理员信息
				group.Group("/admin", func(group *ghttp.RouterGroup) {
					group.POST("info", adminapi.Info)
					group.POST("change_pwd", adminapi.ChangePwd)
				})
				//定时任务
				group.Group("/cron", func(group *ghttp.RouterGroup) {
					group.GET("/", cronapi.GetList)
					group.POST("/", cronapi.CronAdd)
					group.PUT("/", cronapi.CronEdit)
					group.DELETE("/", cronapi.CronDelete)
					group.POST("/run", cronapi.CronRun)
				})
				//定时任务日志
				group.Group("/cron_log", func(group *ghttp.RouterGroup) {
					group.GET("/", cronapi.GetLogList)
					group.GET("/detail", cronapi.GetLogResult)
					group.DELETE("/", cronapi.CronLogDelete)
				})
				//ci任务
				group.Group("/ci", func(group *ghttp.RouterGroup) {
					group.GET("/", ciapi.GetList)
					group.POST("/", ciapi.CiAdd)
					group.PUT("/", ciapi.CiEdit)
					group.DELETE("/", ciapi.CiDelete)
				})
				//ci任务日志
				group.Group("/ci_log", func(group *ghttp.RouterGroup) {
					group.GET("/", ciapi.GetLogList)
					group.GET("/detail", ciapi.GetLogResult)
					group.DELETE("/", ciapi.CiLogDelete)
				})
			})
		})
	})
}
